package com.dxy.df.core.shiro;

import com.dxy.df.constants.ConstStrings;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.*;

@Configuration
public class ShiroConfig {

    private static final long SESSION_TIMEOUT = 12 * 60 * 60 * 1000L;


    @Bean("sessionManager")
    public SessionManager sessionManager() {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setSessionValidationSchedulerEnabled(true);
        sessionManager.setSessionIdUrlRewritingEnabled(false);
        sessionManager.setGlobalSessionTimeout(SESSION_TIMEOUT);
        return sessionManager;
    }

    @Bean("securityManager")
    public SecurityManager securityManager(LoginRealm loginRealm, SessionManager sessionManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        DefaultModularRealm defaultModularRealm = new DefaultModularRealm();
        Map<String, Object> realmsMap = new HashMap<>();
        realmsMap.put("loginRealm", loginRealm);
        defaultModularRealm.setDefinedRealms(realmsMap);
        securityManager.setAuthenticator(defaultModularRealm);
        Collection<Realm> realms = new ArrayList<>();
        realms.add(loginRealm);
        securityManager.setRealms(realms);
        securityManager.setSessionManager(sessionManager);

        return securityManager;
    }

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);

        LoginFilter loginFilter = new LoginFilter();
        ApiFilter apiFilter = new ApiFilter();

        loginFilter.setLoginUrl("/login");

        Map<String, Filter> filters = new HashMap<>();
        filters.put("loginFilter", loginFilter);
        filters.put("apiFilter", apiFilter);
        shiroFilter.setFilters(filters);

        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/v2/**", "anon");
        filterMap.put("/plugins/**", "anon");
        filterMap.put("/webjars/**", "anon");
        filterMap.put("/swagger-resources/**", "anon");
        filterMap.put("/swagger/**", "anon");
        filterMap.put("/swagger-ui.html", "anon");
        filterMap.put("/static/**", "anon");
        filterMap.put("/v2/api-docs", "anon");


        filterMap.put("/**/*.css", "anon");
        filterMap.put("/**/*.js", "anon");
        filterMap.put("/fonts/**", "anon");
        filterMap.put("/favicon.ico", "anon");
        String springProfilesActive =
                System.getProperty(ConstStrings.SPRING_PROFILES_ACTIVE).toLowerCase(Locale.getDefault());
//        if (!"local".equals(springProfilesActive)) {
            filterMap.put("/api/users/login", "loginFilter");
            filterMap.put("/api/**", "apiFilter");
            filterMap.put("/**", "loginFilter");
//        }
        shiroFilter.setFilterChainDefinitionMap(filterMap);

        return shiroFilter;
    }

    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }


    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
        proxyCreator.setProxyTargetClass(true);
        return proxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

}
